package zty.structures.linkedlist;

/**
 * 双向链表
 * @author tyzhang
 * @Date: 2021/2/26 8:58
 */
public class DoubleLinkedList {
    public static void main(String[] args) {
        OnePiece piece1 = new OnePiece(1000,"路飞","超人系");
        OnePiece piece2 = new OnePiece(999,"索隆","剑士");
        OnePiece piece3 = new OnePiece(1234,"乔巴","动物系");
        OnePiece piece4 = new OnePiece(222,"娜美","航海士");
        OnePiece piece5 = new OnePiece(10000,"路飞","觉醒超人系");

        DoubleLinked onePiece = new DoubleLinked();


        onePiece.addNodeByMoney(piece1);
        onePiece.addNodeByMoney(piece2);
        onePiece.addNodeByMoney(piece3);
        onePiece.addNodeByMoney(piece4);
        System.out.println("排序后榜单:");
        onePiece.showList();


        onePiece.updateNode(piece5);
        System.out.println("修改后数据");
        onePiece.showList();

        onePiece.deleteNode("乔巴");
        System.out.println("删除后数据");
        onePiece.showList();
    }
}

class DoubleLinked{

    private final OnePiece head = new OnePiece(0,"","");

    public OnePiece getHead() {
        return head;
    }

    /** 尾插法，添加节点 */
    public void addNode(OnePiece node){
        OnePiece temp = head;
        while (true){
            if (temp.next != null){
                temp = temp.next;
            }else {
                temp.next = node;
                node.pre = temp;
                break;
            }
        }
    }

    /**比较赏金插入数据 */
    public void addNodeByMoney(OnePiece node){
        OnePiece temp = head;
        while (true){
            if (temp.next == null){
                temp.next = node;
                node.pre = temp;
                break;
            }else if (temp.next.money > node.money){
                temp.next.pre = node;
                node.next = temp.next;
                temp.next = node;
                node.pre = temp;
                break;
            }
            temp = temp.next;
        }
    }

    /** 修改节点 */
    public void updateNode(OnePiece node){
        OnePiece temp = head;
        if (temp.next == null){
            System.out.println("链表为空！");
        }else {
            while (true){
                if (temp.next == null){
                    System.out.printf("海贼榜中不存在%s",node.name);
                    break;
                }else if (temp.name.equals(node.name)){
                    temp.money = node.money;
                    temp.ability = node.ability;
                    break;
                }
                temp = temp.next;
            }
        }
    }

    /** 删除节点 */
    public void deleteNode(String name){
        OnePiece temp = head.next;
        if (temp == null){
            System.out.println("海贼榜中无数据！");
        }else {
            while (true){
                if (temp == null){
                    break;
                }
                if (temp.name.equals(name)){
                    if (temp.next != null) {
                        temp.pre.next = temp.next;
                        temp.next.pre = temp.pre;
                    }else {
                        temp.pre.next = null;
                    }
                    break;
                }
                temp = temp.next;
            }
        }
    }

    /** 查看海贼榜 */
    public void showList(){
        if (head.next == null){
            System.out.println("海贼榜为空");
        }
        OnePiece temp = head.next;
        while (temp != null) {
            System.out.println(temp);
//            if (temp.next == null){
//                System.out.println("最后一个海贼的前一个为："+temp.pre);
//            }
            temp = temp.next;
        }
    }
}

class OnePiece{
    public OnePiece pre;
    public int money;
    public String name;
    public String ability;
    public OnePiece next;

    public OnePiece(int money, String name, String ability) {
        this.money = money;
        this.name = name;
        this.ability = ability;
    }

    @Override
    public String toString() {
        return "OnePieceList{" +
                "money=" + money +
                ", name='" + name + '\'' +
                ", ability='" + ability + '\'' +
                '}';
    }
}
